package com.qdsg.modulesihealsdk.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.media.FaceDetector;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

import com.qdsg.modulesihealsdk.R;

import java.util.List;

/**
 * Created by 56228 on 2018-11-21.
 */

public class FaceView extends AppCompatImageView {
    private FaceViewCallback faceViewCallback;

    private FaceImage faceImage;
    private Drawable faceDrawable;

    private RectF mRect = new RectF();

    private boolean canCapture = true;

    public FaceView(Context context) {
        super(context);
        faceDrawable = context.getResources().getDrawable(R.drawable.face_photo);
    }

    public FaceView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        faceDrawable = context.getResources().getDrawable(R.drawable.face_photo);
    }

    public void setFaceViewCallback(FaceViewCallback faceViewCallback) {
        this.faceViewCallback = faceViewCallback;
    }

    public boolean isCanCapture() {
        return canCapture;
    }

    public void setCanCapture(boolean canCapture) {
        this.canCapture = canCapture;
    }

    public void onFacesDetected(FaceImage faceImage) {
        if (!canCapture) {
            return;
        }
        this.faceImage = faceImage;
        invalidate();
    }

    public void onFacesClear() {
//        canCapture = false;
        if (faceImage != null) {
            faceImage = null;
            invalidate();
        }
    }

    private PointF midPoint = new PointF();

    @Override
    protected void onDraw(Canvas canvas) {
        if (faceImage == null) {
            super.onDraw(canvas);
            return;
        }
        List<FaceDetector.Face> faceList = faceImage.getFaceList();
        for (int faceIndex = 0; faceIndex < faceImage.getFaceNum(); faceIndex++) {
            FaceDetector.Face face = faceList.get(faceIndex);
            face.getMidPoint(midPoint);//[386.15,183.89]
            //镜头画面正常状态下
//            midPoint.x = 636.f - midPoint.x;
            float eyesDistance = face.eyesDistance();//91
//            Log.e("midPoint", midPoint.x + "----" + midPoint.y);
            mRect.set((midPoint.x - eyesDistance), (midPoint.y * 3 / 4 - eyesDistance), (midPoint.x + eyesDistance), (midPoint.y * 3 / 4 + eyesDistance * 1.5f));
            faceImage.setFaceRect(faceIndex, mRect);
            faceDrawable.setBounds(Math.round(mRect.left), Math.round(mRect.top),
                    Math.round(mRect.right), Math.round(mRect.bottom));
//            Log.e("faceDrawable", "mRect.left:" + mRect.left + "mRect.right:" + mRect.right + "mRect.top:" + mRect.top + "mRect.bottom:" + mRect.bottom);
            faceDrawable.draw(canvas);
            super.onDraw(canvas);
        }
        if (faceViewCallback != null) {
            faceViewCallback.faceSource(faceImage);
        }
    }
}
